package com.icinfo.cloud.provider.handler;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

@Service
public class ApplicationContextRegister implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextRegister.applicationContext = applicationContext;
    }

    /**
     * 描述: 通过bean的ID获取spring管理的bean对象
     * @auther yushunwei
     * @date 2014-4-15
     * @param beanId
     * @return
     */
    public  Object getBeanById(String beanId){
        return applicationContext.getBean(beanId);
    }

    /**
     * 描述:通过bean的class 获取bean 首字母小写.
     * @author ZhangXiaoYan
     * @date 2017-02-10
     * @param clazz class
     * @return Object
     */
    public  Object getBeanByClass(Class clazz){
        String clazzName = clazz.getSimpleName();
        String beanName=clazzName.replace(clazzName.charAt(0),Character.toLowerCase(clazzName.charAt(0)));
        return applicationContext.getBean(beanName);
    }
}
